//
//  tictactoeViewController.m
//  tictactoe
//
//  Created by iD Student on 6/25/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "tictactoeViewController.h"

@interface tictactoeViewController (){

}
@end

@implementation tictactoeViewController;

@synthesize tl = _tl;
@synthesize tm = _tm;
@synthesize tr = _tr;
@synthesize ml = _ml;
@synthesize m =_m;
@synthesize mr = _mr;
@synthesize bl = _bl;
@synthesize bm = _bm;
@synthesize br = _br;
@synthesize toplabel = _toplabel;
@synthesize bottombutton = _bottombutton;
@synthesize topline = _topline;
@synthesize midline = _midline;
@synthesize bottomline = _bottomline;
@synthesize leftline = _leftline;
@synthesize upline = _upline;
@synthesize rightline = _rightline;
@synthesize diagonallc = _diagonallc;
@synthesize diagonalright = _diagonalright;



- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.tl.text = @" ";
    self.tm.text = @" ";
    self.tr.text = @" ";
    self.ml.text = @" ";
    self.m.text = @" ";
    self.mr.text = @" ";
    self.bl.text = @" ";
    self.bm.text = @" ";
    self.br.text = @" ";
    self.toplabel.text = @"X's turn.";
    _bottombutton.hidden = YES;
    _topline.hidden =YES;
    _midline.hidden =YES;
    _bottomline.hidden =YES;
    _leftline.hidden =YES;
    _upline.hidden =YES;
    _rightline.hidden =YES;
    _diagonallc.transform = CGAffineTransformMakeRotation(3.14/4.5);
    _diagonallc.hidden = YES;
    _diagonalright.transform = CGAffineTransformMakeRotation(9.42/3.85);
    _diagonalright.hidden = YES;
    
}

- (void)viewDidUnload
{
    [self setTl:nil];
    [self setTm:nil];
    [self setTr:nil];
    [self setMl:nil];
    [self setM:nil];
    [self setMr:nil];
    [self setBl:nil];
    [self setBm:nil];
    [self setBr:nil];
    [self setToplabel:nil];
 
    [self setBottombutton:nil];
    [self setTopline:nil];
    [self setMidline:nil];
    [self setBottomline:nil];
    [self setLeftline:nil];
    [self setUpline:nil];
    [self setRightline:nil];
    [self setDiagonallc:nil];
    [self setDiagonalright:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if(buttonIndex == 0){
        _bottombutton.hidden= YES;
        self.tl.text = @" ";
        self.tm.text = @" ";
        self.tr.text = @" ";
        self.ml.text = @" ";
        self.m.text = @" ";
        self.mr.text = @" ";
        self.bl.text = @" ";
        self.bm.text = @" ";
        self.br.text = @" ";
        ntl=0;
        ntm=0;
        ntr=0;
        nml=0;
        nm=0;
        nmr=0;
        nbl=0;
        nbm=0;
        nbr=0;
        win=0;
        _topline.hidden =YES;
        _midline.hidden =YES;
        _bottomline.hidden =YES;
        _leftline.hidden =YES;
        _upline.hidden =YES;
        _rightline.hidden =YES;
        _diagonallc.hidden = YES;
        _diagonalright.hidden =YES;
        
        if(num%2==0){
            self.toplabel.text = @"O's turn.";
        }
        else{
            self.toplabel.text = @"X's turn.";
        }
    }
}
int num=3;

int ntl;
int ntm;
int ntr;
int nml;
int nm;
int nmr;
int nbl;
int nbm;
int nbr;
int win;

- (IBAction)leftcorner {
    if(ntl==0&&win!=1){
    if(num%2==1){
        self.tl.text= @"X";
        ntl=1;
        self.toplabel.text = @"O's turn.";
        if(ntl==1&&ntm==1&&ntr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _topline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        if(ntl==1&&nml==1&&nbl==1){
            self.toplabel.text = @"X won.";
            win=1;
            _leftline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }  
        if(ntl==1&&nm==1&&nbr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _diagonallc.hidden = NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
    }
    
    else{
        self.tl.text=@"O";
        ntl=2;
        self.toplabel.text = @"X's turn.";
        if(ntl==2&&ntm==2&&ntr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _topline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        if(ntl==2&&nml==2&&nbl==2){
            self.toplabel.text = @"O won.";
            win=1;
            _leftline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }  
        if(ntl==2&&nm==2&&nbr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _diagonallc.hidden = NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
    }
    }
}

- (IBAction)topmiddle {
    if(ntm==0&&win!=1){
    if(num%2==1){
        self.tm.text= @"X";
        ntm=1;
        self.toplabel.text = @"O's turn.";
        if(ntl==1&&ntm==1&&ntr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _bottombutton.hidden = NO;
            _topline.hidden = NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
           
 
        }
        if(ntm==1&&nm==1&&nbm==1){
            self.toplabel.text = @"X won.";
            win=1;
            _upline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    else{
        self.tm.text=@"O";
        ntm=2;
        self.toplabel.text = @"X's turn.";
        if(ntl==2&&ntm==2&&ntr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _topline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        if(ntm==2&&nm==2&&nbm==2){
            self.toplabel.text = @"O won.";
            win=1;
            _upline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
    }
    }
}

- (IBAction)topright {
    if(ntr==0&&win!=1){
    if(num%2==1){
        self.tr.text= @"X";
        ntr=1;
        self.toplabel.text = @"O's turn.";
        if(ntl==1&&ntm==1&&ntr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _topline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        if(ntr==1&&nmr==1&&nbr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _rightline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntr==1&&nm==1&&nbl==1){
            self.toplabel.text = @"X won.";
            win=1;
             _diagonalright.hidden = NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    else{
        self.tr.text=@"O";
        ntr=2;
        self.toplabel.text = @"X's turn.";
        if(ntl==2&&ntm==2&&ntr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _topline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        if(ntr==2&&nmr==2&&nbr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _rightline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntr==2&&nm==2&&nbl==2){
            self.toplabel.text = @"O won.";
            win=1;
            _diagonalright.hidden = NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    }}

- (IBAction)middleleft {
    if(nml==0&&win!=1){
    if(num%2==1){
        self.ml.text= @"X";
        nml=1;

        self.toplabel.text = @"O's turn.";
        if(nml==1&&nm==1&&nmr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _midline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
            
                    
        }
        if(ntl==1&&nml==1&&nbl==1){
            self.toplabel.text = @"X won.";
            win=1;
            _leftline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];

        }
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    else{
        self.ml.text=@"O";
        nml=2;
        self.toplabel.text = @"X's turn.";
        if(nml==2&&nm==2&&nmr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _midline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        if(ntl==2&&nml==2&&nbl==2){
            self.toplabel.text = @"O won.";
            win=1;
            _leftline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    }
}

- (IBAction)middle {
    if(nm==0&&win!=1){
    if(num%2==1){
        self.m.text= @"X";
        nm=1;

        self.toplabel.text = @"O's turn.";
        if(nml==1&&nm==1&&nmr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _midline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntm==1&&nm==1&&nbm==1){
            self.toplabel.text = @"X won.";
            win=1;
            _upline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl==1&&nm==1&&nbr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _diagonallc.hidden = NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntr==1&&nm==1&&nbl==1){
            self.toplabel.text = @"X won.";
            win=1;
            _diagonalright.hidden = NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    else{
        self.m.text=@"O";
        nm=2;
        self.toplabel.text = @"X's turn.";
        if(nml==2&&nm==2&&nmr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _midline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        if(ntm==2&&nm==2&&nbm==2){
            self.toplabel.text = @"O won.";
            win=1;
            _upline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl==2&&nm==2&&nbr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _diagonallc.hidden = NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntr==2&&nm==2&&nbl==2){
            self.toplabel.text = @"O won.";
            win=1;
            _diagonalright.hidden = NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    }
}

- (IBAction)middleright {
    if(nmr==0&&win!=1){
    if(num%2==1){
        self.mr.text= @"X";
        nmr=1;
        
        self.toplabel.text = @"O's turn.";
      if(nml==1&&nm==1&&nmr==1){
            self.toplabel.text = @"X won.";
            win=1;
          _midline.hidden =NO;
            _bottombutton.hidden= NO;
          UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
          [alert show];
        }  
        if(ntr==1&&nmr==1&&nbr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _rightline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    else{
        self.mr.text=@"O";
        nmr=2;
        self.toplabel.text = @"X's turn.";
        if(nml==2&&nm==2&&nmr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _midline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        if(ntr==2&&nmr==2&&nbr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _rightline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    }
}

- (IBAction)bottomleft {
    if(nbl==0&&win!=1){
    if(num%2==1){
        self.bl.text= @"X";
        nbl=1;
        self.toplabel.text = @"O's turn.";
        if(nbl==1&&nbm==1&&nbr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _bottomline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }  
        if(ntl==1&&nml==1&&nbl==1){
            self.toplabel.text = @"X won.";
            win=1;
            _leftline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }  
        if(ntr==1&&nm==1&&nbl==1){
            self.toplabel.text = @"X won.";
            win=1;
            _diagonalright.hidden = NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    else{
        self.bl.text=@"O";
        nbl=2;
        self.toplabel.text = @"X's turn.";
        if(nbl==2&&nbm==2&&nbr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _bottomline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }  
        if(ntl==2&&nml==2&&nbl==2){
            self.toplabel.text = @"O won.";
            win=1;
            _leftline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntr==2&&nm==2&&nbl==2){
            self.toplabel.text = @"O won.";
            win=1;
            _diagonalright.hidden = NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    }
}

- (IBAction)bottommiddle {
    if(nbm==0&&win!=1){
    if(num%2==1){
        self.bm.text= @"X";
        nbm=1;
        self.toplabel.text = @"O's turn.";
        if(nbl==1&&nbm==1&&nbr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _bottomline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }  
        if(ntm==1&&nm==1&&nbm==1){
            self.toplabel.text = @"X won.";
            win=1;
            _upline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    else{
        self.bm.text=@"O";
        nbm=2;
        self.toplabel.text = @"X's turn.";
        if(nbl==2&&nbm==2&&nbr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _bottomline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }  
        if(ntm==2&&nm==2&&nbm==2){
            self.toplabel.text = @"O won.";
            win=1;
            _upline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    }
}

- (IBAction)bottomright {
    if(nbr==0&&win!=1){
    if(num%2==1){
        self.br.text= @"X";
        nbr=1;
        self.toplabel.text = @"O's turn.";
        if(nbl==1&&nbm==1&&nbr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _bottomline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }  
        if(ntr==1&&nmr==1&&nbr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _rightline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl==1&&nm==1&&nbr==1){
            self.toplabel.text = @"X won.";
            win=1;
            _diagonallc.hidden = NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"X has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    else{
        self.br.text=@"O";
        nbr=2;
        self.toplabel.text = @"X's turn.";
        if(nbl==2&&nbm==2&&nbr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _bottomline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }  
        if(ntr==2&&nmr==2&&nbr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _rightline.hidden =NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl==2&&nm==2&&nbr==2){
            self.toplabel.text = @"O won.";
            win=1;
            _diagonallc.hidden = NO;
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"O has won!!" message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        } 
        if(ntl>0&&ntm>0&&ntr>0&&nml>0&&nm>0&&nmr>0&&nbl>0&&nbm>0&&nbr>0&&win!=1){
            self.toplabel.text=@"Tie.";
            _bottombutton.hidden= NO;
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Tie." message:nil delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit" ,nil];
            [alert show];
        }
        num++;
        
    }
    }
}

- (IBAction)start {
    _bottombutton.hidden= YES;
    self.tl.text = @" ";
    self.tm.text = @" ";
    self.tr.text = @" ";
    self.ml.text = @" ";
    self.m.text = @" ";
    self.mr.text = @" ";
    self.bl.text = @" ";
    self.bm.text = @" ";
    self.br.text = @" ";
    ntl=0;
    ntm=0;
    ntr=0;
    nml=0;
    nm=0;
    nmr=0;
    nbl=0;
    nbm=0;
    nbr=0;
    win=0;
    _topline.hidden =YES;
    _midline.hidden =YES;
    _bottomline.hidden =YES;
    _leftline.hidden =YES;
    _upline.hidden =YES;
    _rightline.hidden =YES;
    _diagonallc.hidden = YES;
    _diagonalright.hidden =YES;
    
    if(num%2==0){
    self.toplabel.text = @"O's turn.";
    }
    else{
        self.toplabel.text = @"X's turn.";
    }
}
- (IBAction)win {
    if(ntl==1&&ntm==1&&ntr==1){
        self.toplabel.text = @"X won.";
        win=1;
    }
    
    if(win==1){
        _bottombutton.hidden= NO;
    }
}
@end
